;(function($) {
    //定义的构造函数http://www.jq22.com/jquery-info5844
	var documents=$(document)
    var Drag = function(ele, opt) {
        this.$ele = ele,this.x = 0,this.y = 0
        this.defaults = {
					parent:'parent',//定义拖动不能超出的外框,拖动范围
					direction:'all',//方向
					//closes:1,//是否能关闭
					handler:false,//把手
					bordercolor:"blue",//把手
					changewh:false,//拖动改变div宽高的把手
					dragStart:function(x,y){},//拖动开始 x,y为当前坐标
					dragEnd:function(x,y){},//拖动停止 x,y为当前坐标
					dragMove:function(x,y){},//拖动进行中 x,y为当前坐标
				},
		this.options = $.extend({}, this.defaults, opt)
    }
    //定义方法
    Drag.prototype = {
        run: function() {
					var $this = this;
					var element = this.$ele;
					var direction = this.options.direction; //方向
					var handler = this.options.handler;
					var initxy = this.options.initxy;
					var closes = this.options.closes;
					var bordercolor = this.options.bordercolor;
					var parent = this.options.parent;
					var isDown = false; //记录鼠标是否按下
					var fun = this.options; //使用外部函数
					var X = 0,Y = 0,moveX = 0,moveY = 0;
					// 阻止冒泡
					var xvDrag={};
					xvDrag.changewh=this.options.changewh;
					xvDrag.isDown=false;//是否按下拖拽高度的手把

					element.find('*').not('img').mousedown(function(e) {
						e.stopPropagation();
					});
					
					//初始化判断
					if(parent == 'parent'){
						parent = element.parent();
					}else{
						parent = element.parents(parent);
					}
					if(!handler){
						handler = element; 
					}else{
						handler = element.find(handler);
					}
					
					//初始化
					//parent.css({position:'relative'});
					
//					if(closes){
//						element.find(".ul").append("<li> X </li>");
//							element.on("click","li",function(){
//							element.remove()
//							})
//						}
					
					element.css({position:'absolute',border:'1px dashed '+bordercolor});
					element.data({x:0,y:0});//位置
					
					var inits={}
					inits.boxWidth=0;
					inits.boxHeight=0;
					inits.sonWidth=0;
					inits.sonHeight=0;
					
					//盒子 和 元素大小初始化
					initSize();
					
					//盒子 和 元素大小初始化函数
					function initSize(){
						inits.boxWidth = parent.outerWidth();
						inits.boxHeight = parent.outerHeight();
						inits.sonWidth = element.outerWidth();
						inits.sonHeight = element.outerHeight();
					}
					
					if(xvDrag.changewh)//拖动改变宽高
					{
						var mouseStart={};
						var divStart={};
						element.find(".ul").append("<span style='background-color:"+bordercolor+"'></span>")
						element.find("span").mousedown(function(e){
							e.stopPropagation();
							xvDrag.isDown=true;
							mouseStart.x=e.pageX;
							mouseStart.y=e.pageY;
							divStart.w=element.outerWidth();
							divStart.h=element.outerHeight();
							divStart.x = element.offset().left;
							divStart.y = element.offset().top;
						});
						
						
						documents.on("mousemove",parent,function(e){
							if(!xvDrag.isDown){return false;}
							var w=divStart.w+e.pageX-mouseStart.x;
							var h=divStart.h+e.pageY-mouseStart.y;

							
							var parents={}
							parents.x = parent.offset().left;
							parents.y = parent.offset().top;
							
							if(divStart.x+w>parents.x+parent.outerWidth()) w = parents.x+parent.width() - divStart.x;
							if(divStart.y+h>parents.y+parent.outerHeight()) h = parents.y+parent.height() - divStart.y;

							if(w>18) element.width(w);
							if(h>18) element.height(h);
						})
					
						documents.on("mouseup","body",function(){
							element.data({x:parseInt(element.css('left')),y:parseInt(element.css('top'))});//位置
							xvDrag.isDown=false;
						})
						
					}
					
					handler.css({cursor:'move'});
					handler.mousedown(function(e){
						e.stopPropagation();
						isDown = true;
						element.addClass('doing');
						X = e.pageX;
						Y = e.pageY;
						$this.x = element.position().left;
						$this.y = element.position().top;
						initSize();
						fun.dragStart(parseInt(element.css('left')),parseInt(element.css('top')));
						return false;
					});
					documents.on("mouseup","body",function(){
						if(!isDown){return;};
						fun.dragEnd(parseInt(element.css('left')),parseInt(element.css('top')));element.removeClass('doing');
						element.data({x:parseInt(element.css('left')),y:parseInt(element.css('top'))});//位置
						isDown = false;
					})
					documents.on("mousemove",parent,function(e){
						//拖拽位置
						if(!isDown){return;}
						//
						moveX = $this.x+e.pageX-X;
						moveY = $this.y+e.pageY-Y;
						function thisXMove(){ //x轴移动
							element.css({left:moveX});
							if(moveX < 0){
								element.css({left:0});
							}
							if(moveX > (inits.boxWidth-inits.sonWidth)){
								element.css({left:inits.boxWidth-inits.sonWidth});
							}
							return moveX;
						}
						function thisYMove(){ //y轴移动
						element.css({top:moveY});
							if(moveY < 0){
								element.css({top:0});
							}
							if(moveY > (inits.boxHeight-inits.sonHeight)){
								element.css({top:inits.boxHeight-inits.sonHeight});
							}
							return moveY;
						}
						function thisAllMove(){ //全部移动
							element.css({left:moveX,top:moveY});
							if(moveX < 0){
								element.css({left:0});
							}
							if(moveX > (inits.boxWidth-inits.sonWidth)){
								element.css({left:inits.boxWidth-inits.sonWidth});
							}
							if(moveY < 0){
								element.css({top:0});
							}
							if(moveY > (inits.boxHeight-inits.sonHeight)){
								element.css({top:inits.boxHeight-inits.sonHeight});
							}
						}
						
						fun.dragMove(parseInt(element.css('left')),parseInt(element.css('top')));
						if(direction.toLowerCase() == "x"){
							thisXMove();
						}else if(direction.toLowerCase() == "y"){
							thisYMove();
						}else{
							thisAllMove();
						}
					});
        }
    }

    //插件
    $.fn.myDrag = function(options) {
		(new Drag(this, options)).run();
		//return this
    }
})(jQuery);